#include "PreCompile.hpp"
#include "GPathExtTests.hpp"
#include "../g_path_ext.h"

namespace BuildNG
{
  static void make_slashes_native(Glib::ustring &str)
  {
    for(Glib::ustring::iterator it = str.begin();it != str.end();++it)
    {
      if(*it == '/')
      {
        Glib::ustring::iterator end = it;
        end++;
        str.replace(it,end,G_DIR_SEPARATOR_S);
      }
    }
  }

  void GPathExtTests::test_get_relative_path()
  {
    Glib::ustring abs_path = "/home/user/dir/lalaa.xml";
    Glib::ustring relative_to_this = "/home/user";
    Glib::ustring expected_res = "dir/lalaa.xml";

    make_slashes_native(expected_res);
    
    assert(Glib::Path::get_relative_path(abs_path,relative_to_this) == expected_res);
    
    abs_path = "/home/user/dir/lalaa.xml";
    relative_to_this = "/home/user2";
    expected_res = "../user/dir/lalaa.xml";

    make_slashes_native(expected_res);
    
    assert(Glib::Path::get_relative_path(abs_path,relative_to_this) == expected_res);

    abs_path = "/home/user/lalaa";
    relative_to_this = "/home/user/lalaa";
    expected_res = "";
    make_slashes_native(expected_res);
    assert(Glib::Path::get_relative_path(abs_path,relative_to_this) == expected_res);
  }
  
  void GPathExtTests::test_canonicalize_path()
  {
    for(int i = 0;i < 2;i++)
    {
      Glib::ustring expected_res = "c:\\BuildNG\\systemdev2\\src\\CLang\\CLangTests\\TestFiles";
      make_slashes_native(expected_res);

      assert(Glib::Path::canonicalize_path(
        "c:\\BuildNG\\systemdev2\\src\\CLang\\CLangTests\\TestFiles",i ? CPF_WINDOWS_PATH : CPF_UNIX_PATH) == expected_res);
    }

    {
      Glib::ustring expected_res = "/path2/lalaaa.xml";
      make_slashes_native(expected_res);

      assert(Glib::Path::canonicalize_path(
        "/test/..//./path2///./lalaaa.xml",CPF_UNIX_PATH) == expected_res);
    }

    {
      Glib::ustring expected_res = "path2/lalaaa.xml";
      make_slashes_native(expected_res);

      assert(Glib::Path::canonicalize_path(
        "test/..//./path2///./lalaaa.xml",CPF_UNIX_PATH) == expected_res);
    }
  }
  
  void GPathExtTests::test_split_into_elements()
  {
    for(int i = 0;i < 3;i++)
    {
      Glib::ustring str = "a/bb//cccc///ddd/e";
      for(int j = 0;j < i;j++)
        str += "/";
      
      std::vector<Glib::ustring> elements = Glib::Path::split_into_elements(str);
      assert(elements.size() == 5);
      assert(elements[0] == "a");
      assert(elements[1] == "bb");
      assert(elements[2] == "cccc");
      assert(elements[3] == "ddd");
      assert(elements[4] == "e");
    }

    assert(Glib::Path::split_into_elements("/").size() == 0);
  }
  
  void GPathExtTests::test_split_into_elements_empty_str()
  {
    std::vector<Glib::ustring> elements = Glib::Path::split_into_elements("");
    assert(elements.size() == 0);
    
    elements = Glib::Path::split_into_elements("/");
    assert(elements.size() == 0);
    
    Glib::Path::split_into_elements("//");
    assert(elements.size() == 0);
    
    Glib::Path::split_into_elements("///");
    assert(elements.size() == 0);
  }
}
